<template>
  <div
    class="flex-ai-c"
    :class="{'switch-button': isButton,'switch-tab':!isButton,[buttonType]:isButton}"
  >
    <!-- 待优化文字数量偏少时切换至数量多的文字 存在宽度抖动问题 -->
    <div
      v-for="tab in tabList"
      :key="tab.value"
      class="switch-tab-item"
      :class="{'switch-active':tab.value === value}"
      @click="handleChangeTab(tab)"
    >
      <span>{{ tab.label }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SwicthTab',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    isButton: {
      type: Boolean,
      default: false
    },
    buttonType: { // 仅在 isButton=true 下生效
      type: String,
      default: 'short'
    },
    value: {
      type: String || Number,
      default: ''
    },
    tabList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      handleChangeTab (tab) {
        this.$emit('change', tab.value)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.switch-tab {
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0 4px;

  .switch-tab-item {
    font-size: 14px;
    height: 26px;
    color: #fff;
    cursor: pointer;

    &.switch-active {
      font-family: "Microsoft YaHei", "Microsoft YaHei-Bold", serif;
      font-weight: 700;
      text-align: left;
      color: #00f6ff;
      position: relative;

      &::after {
        content: "";
        position: absolute;
        bottom: 2px;
        left: 50%;
        transform: translateX(-50%);
        background: url("~@/assets/images/ysqjcdp/swicth-avtive.png") no-repeat left top;
        background-size: 100% 100%;
        width: calc(100% + 2px);
        height: 12px;
      }
    }
  }
}

.switch-button {
  height: 26px;
  width: fit-content;

  &.short {
    .switch-tab-item {
      width: 80px;
      background: url("~@/assets/images/ysqjcdp/switch-tab-button.png") no-repeat left top;
      background-size: 100% 100%;

      &:not(:first-child) {
        position: relative;
        left: -5px;
      }
    }

    .switch-tab-item.switch-active {
      background: url("~@/assets/images/ysqjcdp/switch-tab-button-active.png") no-repeat left top;
      background-size: 100% 100%;
    }
  }

  &.long {
    .switch-tab-item {
      width: 130px;
      background: url("~@/assets/images/ysqjcdp/switch-tab-button-long.png") no-repeat left top;
      background-size: 100% 100%;

      &:not(:first-child) {
        position: relative;
        left: -10px;
      }
    }

    .switch-tab-item.switch-active {
      background: url("~@/assets/images/ysqjcdp/switch-tab-button-long-active.png") no-repeat left top;
      background-size: 100% 100%;
    }
  }

  .switch-tab-item {
    height: 24px;
    line-height: 24px;
    font-size: 16px;
    font-family: YouSheBiaoTiHei, YouSheBiaoTiHei-Regular, serif;
    font-weight: 400;
    text-align: center;
    opacity: 0.5;
    color: #fff;
    cursor: pointer;

    &.switch-active {
      opacity: 1;

      &::after {
        content: "";
      }
    }
  }
}
</style>
